__author__ = 'el_even'

from selenium import webdriver



from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.common.by import By

from time import sleep

from browsermobproxy import Server
import omnistructure
import path
import launcher
import generator
from pages.flatreg import FlatRegPage

#from pages.forum import ForumPage
#import pages.forum


def __init__(self, driver, base_url):
    #driver.get(base_url)
    self.wait = WebDriverWait(driver, 5)
#    self.fp = ForumPage(driver, base_url)


server = Server(path.browsermob)
profile = webdriver.FirefoxProfile()

server.start()
proxy = server.create_proxy()

driver = webdriver.Firefox(firefox_profile=profile, proxy=proxy.selenium_proxy())
driver.set_window_size(880, 1010)
driver.set_window_position(800, 0)


omnidic = {}  # the dictionary with useful omniture props and vars


page = launcher.page1

def starthar(page):
    # 'har_name' looks like useless
    har_name = page
    proxy.new_har(har_name)

def omniparser():
    #omnidic = {}
    omnitempdic = {}  # the dictionary with all omniture props and vars
    headers = proxy.har

    for i in range(0, len(headers['log']['entries'])-1):
        # cutting off useless items
        if headers['log']['entries'][i]['request']['queryString'] != []:
            # filtering omniture requests
            if headers['log']['entries'][i]['request']['queryString'][0]['name'] == 'fid':
                omniquery = headers['log']['entries'][i]['request']['queryString']
                for k in range(0, len(omniquery)):
                    omnitempdic[omniquery[k]['name']] = omniquery[k]['value']
    print '\nomnitempdic:', omnitempdic

    for i in range(0, len(omnitempdic)):
        if omnitempdic.keys()[i] in omnistructure.omniheaders.keys():
            omnidic[omnistructure.omniheaders.get(omnitempdic.keys()[i])] = omnitempdic.values()[i]
    print 'omnidic:', omnidic

def omnichecker(getaction):
    # i guess i can get rid of 'action'
    action = getaction
    print action
    for i in range(0, len(omnidic)):
        tempkey = omnidic.keys()[i]
        if tempkey in omnistructure.mode.get(action).keys():
            if omnidic.get(tempkey) == omnistructure.mode.get(action).get(tempkey):
                print '^_^',
            else:
                print 'x_x',
            print tempkey, 'G:', omnidic.get(tempkey), 'N:', omnistructure.mode.get(action).get(tempkey)
        else:
            print 'O_o', tempkey, 'G:', omnidic.get(tempkey), 'N: NO NEED'


def stopproxy():
    proxy.close()
    server.stop()
    driver.quit()



starthar(launcher.page1)

driver.get(path.penton.get(page))
omniparser()
omnichecker(launcher.page1)

if launcher.mode == 'forum':
    driver.find_element(By.XPATH, ".//div[@id='pm-main-pdg']/h4/a[2]").click()
elif launcher.mode == 'comment':
    driver.find_element(By.XPATH, ".//div[@id='pm-main-pdg']/h4/a[2]").click()
elif launcher.mode == 'nav':
    driver.find_element(By.XPATH, ".//div[@id='block-penton_custom-login_logout']/div/div/div/div[1]/a[1]").click()
elif launcher.mode == 'email':
    driver.find_element(By.XPATH, ".//div[@id='node-47027']/div[4]/div[2]/ul/li[1]/ul/li/a").click()
elif launcher.mode == 'print':
    driver.find_element(By.CSS_SELECTOR, ".print-page.is-processed").click()
elif launcher.mode == 'download':
    driver.find_element(By.CSS_SELECTOR, ".datasheet-filesize").click()
elif launcher.mode == 'gated':
    driver.find_element(By.XPATH, ".//div[@id='node-49808']/div[3]/div/div[2]/div/div[2]/div[1]/a").click()

#starthar(launcher.page2)
omnidic = {}
omniparser()
omnichecker(launcher.page1)

name = driver.find_element(By.XPATH, ".//input[@id='edit-name']")
name.click()
name.clear()
nametxt = generator.name
name.send_keys(nametxt)

email = driver.find_element(By.XPATH, ".//input[@id='edit-mail']")
email.click()
email.clear()
email.send_keys(generator.email)

pwd = driver.find_element(By.XPATH, ".//input[@id='edit-pass']")
pwd.click()
pwd.clear()
pwd.send_keys(generator.pwd)

if launcher.mode == 'email':
    recipient = driver.find_element(By.XPATH, ".//input[@id='edit-recipients']")
    recipient.click()
    recipient.clear()
    recipient.send_keys(generator.recipient)

submit = driver.find_element(By.XPATH, ".//input[@id='edit-submit']")
submit.click()

#starthar(launcher.page3)
omnidic = {}
omniparser()
omnichecker(launcher.page2)

#driver.implicitly_wait(10)
sleep(10)

omnidic = {}
omniparser()
omnichecker(launcher.page3)


stopproxy()
